<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record, text }">
        <template v-if="column.key === 'cpNo'">
          <Tag color="green">
            {{ record.cpNo }}
          </Tag>
        </template>
      </template>
      <template #toolbar>
        <a-button
          :preIcon="IconEnum.ADD"
          v-auth="CpAuth.ADD"
          @click="handleCreate"
          type="primary"
        >
          新增
        </a-button>
        <a-button
          :preIcon="IconEnum.DELETE"
          v-auth="CpAuth.DELETE"
          @click="handleDelete"
          type="primary"
          color="error"
        >
          删除
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: IconEnum.AUTH,
              tooltip: '业务授权',
              auth: CpAuth.SINGLE,
              onClick: handleResAuthBus.bind(null, record),
              color: 'warning'
            },
            {
              icon: IconEnum.AUTH,
              tooltip: '玩法授权',
              auth: CpAuth.SINGLE,
              onClick: handleResAuthPlay.bind(null, record),
              color: 'success'
            },
            {
              icon: IconEnum.VIEW,
              tooltip: '查看',
              auth: CpAuth.SINGLE,
              onClick: handleView.bind(null, record),
            },
            {
              icon: IconEnum.EDIT,
              tooltip: '编辑',
              auth: CpAuth.EDIT,
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: IconEnum.DELETE,
              tooltip: '删除',
              auth: CpAuth.DELETE,
              color: 'error',
              onClick: handleDelete.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <CpModal @register="registerModal" @success="handleSuccess"/>
    <CpModalAuthBus @register="registerResAuthBusModal" @success="handleSuccess"/>
    <CpModalAuthPlay @register="registerResAuthPlayModal"/>
  </div>
</template>

<script setup lang="ts">
import {reactive} from 'vue';
import {listCpApi, delCpApi} from '@/api/core/cp.api';
import {useModal} from '@/components/Modal';
import CpModal from './CpModal.vue';
import {useMessage} from '@/hooks/web/useMessage';
import {IconEnum} from '@/enums/basic';
import {BasicTable, TableAction, useTable} from '@/components/Table';
import {CpAuth} from '@/auth/core/cp.auth';
import {columns, searchFormSchema} from './cp.data';
import {CpDetailGo} from '@/enums/core/cp.enum';
import {useUserStore} from '@/store/modules/user';
import {useForm} from "@/components/Form";
import CpModalAuthBus from "@/views/core/cp/CpModalAuthBus.vue";
import {Tag} from 'ant-design-vue';
import CpModalAuthPlay from "@/views/core/cp/CpModalAuthPlay.vue";

const {createMessage, createConfirm} = useMessage();
const [registerForm, {updateSchema}] = useForm();
const [registerModal, {openModal}] = useModal();
const [registerResAuthBusModal, {openModal: openResAuthBusModal}] = useModal();
const [registerResAuthPlayModal, {openModal: openResAuthPlayModal}] = useModal();
const state = reactive<{
  ids: string[];
  idNames: string;
}>({
  ids: [],
  idNames: '',
});

const [registerTable, {reload}] = useTable({
  title: '信息管理列表',
  api: listCpApi,
  striped: false,
  useSearchForm: true,
  rowKey: 'id',
  bordered: true,
  showIndexColumn: true,
  columns,
  formConfig: {
    labelWidth: 120,
    schemas: searchFormSchema,
  },
  showTableSetting: true,
  tableSetting: {
    fullScreen: true,
  },
  actionColumn: {
    width: 220,
    title: '操作',
    dataIndex: 'action',
    slots: {customRender: 'action'},
  },
  rowSelection: {
    onChange: (selectedRowKeys, selectRows) => {
      state.ids = selectedRowKeys as string[];
      state.idNames = selectRows
        .map((item) => {
          return item.id;
        })
        .join(',');
    },
  },
});

/** 查看按钮 */
function handleView(record: Recordable) {
  useUserStore().getRoutePath(CpDetailGo, record?.id);
}

/** 新增按钮 */
function handleCreate() {
  openModal(true, {
    isUpdate: false,
  });
}

/** 修改按钮 */
function handleEdit(record: Recordable) {
  openModal(true, {
    record,
    isUpdate: true,
  });
}

/** 删除按钮 */
function handleDelete(record: Recordable) {
  const delIds = record?.id || state.ids;
  const delNames = record?.id || state.idNames;
  if (!record?.id && state.ids.length === 0) {
    createMessage.warning('请选择要操作的数据！');
  } else {
    createConfirm({
      iconType: 'warning',
      title: '提示',
      content: '是否确定要删除' + delNames + '?',
      onOk: () =>
        delCpApi(delIds).then(() => {
          createMessage.success('删除' + delNames + '成功！');
          reload();
        }),
    });
  }
}

function handleSuccess() {
  reload();
}


/** 业务授权 */
function handleResAuthBus(record: Recordable) {
  openResAuthBusModal(true, {
    record,
  });
}

/**
 * 玩法授权
 */
function handleResAuthPlay(record: Recordable) {
  openResAuthPlayModal(true, {
    record,
  });
}
</script>
